package com.woniu.p2p.behindController;

import com.github.pagehelper.PageInfo;
import com.woniu.p2p.behindService.AnnounceService;
import com.woniu.p2p.entity.Show;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.Map;

/**
 * AnnounceController
 *
 */
@Controller
@RequestMapping("/announce")
public class AnnounceController {


    @Autowired
    AnnounceService announceService;

    //查询实时咨询
    @RequestMapping("/selectAnnounce")
    public String selectNewsRecord(Map map, String announceName, Integer pageNum){
        Integer start=pageNum==null?1:pageNum;
        PageInfo<Show> pageInfo = announceService.selectByName(announceName, start);
        map.put("announceName",announceName);
        map.put("announces",pageInfo);
        return "content/announce";
    }


    /**
     * 添加网络公告
     * @param title
     * @param context
     * @return
     */
    @RequestMapping("/addAnnounce")
    public String addAnnounce(String title,String context){
        Show announce=new Show();
        announce.setShowName(title);
        announce.setShowContext(context);
        announce.setShowDate(new Date());
        announceService.addAnnounce(announce);
        return "redirect:/announce/selectAnnounce";
    }

    @RequestMapping("/delAnnounce")
    public String delAnnounce(String ids){
        String[] id = ids.split(",");
        for (String string : id) {
            Integer announceId=Integer.parseInt(string);
            announceService.delectAnnounce(announceId);
        }

        return "redirect:/announce/selectAnnounce";
    }

    @RequestMapping("/updateAnnounce")
    public String updateAnnounce(String mytitle,String mycontext,Integer announceId){
        Show announce=new Show();
        announce.setShowName(mytitle);
        announce.setShowContext(mycontext);
        announce.setShowId(announceId);
        announce.setShowDate(new Date());
        announceService.updateAnnounce(announce);
        return "redirect:/announce/selectAnnounce";
    }
}
